package com.yd.controller.system;


import java.io.IOException;
import java.net.MalformedURLException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yd.controller.yd.YD_Users;
import com.yd.pojo.MoneyRecord;
import com.yd.service.MoneyRecordService;
import com.yd.tool.UtsReg;


@Controller
public class MoneyRecordControllerSys {

	@Resource
	private MoneyRecordService moneyrecordService;
	
	//结款记录表list
		@RequestMapping("/sys/moneyrecord/list")
		@ResponseBody
		public String sys_moneyrecord_list(HttpServletRequest request) {
			JSONObject jsonObject = new JSONObject();
			String so = "";
			String page = "";
			String size = "";
			if (UtsReg.getStringKB(request.getParameter("so"))) {so = request.getParameter("so").trim();}
			if (UtsReg.getIntXl(request.getParameter("page"))) {page = request.getParameter("page").trim();}{page = "1";}
			if (UtsReg.getStringKB(request.getParameter("size"))) {size = request.getParameter("size").trim();}{size = "10";}

			
			PageHelper.startPage(Integer.valueOf(page), Integer.valueOf(size));
	        List<MoneyRecord> list = null;
			EntityWrapper<MoneyRecord> ew = new EntityWrapper<MoneyRecord>();
			if (so.length() > 0) {ew.like("title", so);}
			list = moneyrecordService.selectList(ew.orderBy("id", false));
	        PageInfo<MoneyRecord> pages = new PageInfo<MoneyRecord>(list);

			jsonObject.put("data", pages);
			jsonObject.put("error", "0");
			jsonObject.put("msg", "成功");

			return jsonObject.toString();
			

		}
		//增加
		@RequestMapping("/sys/moneyrecord/add")
		@ResponseBody
		public String sys_moneyrecord_add(HttpServletRequest request) {
			JSONObject jsonObject = new JSONObject();


			String shoukuanzhanghao="";
			String dakuanzhanghao="";
			String price="";
			String usersid="";
			String chaozuoyuanid="";
			
			if(UtsReg.getStringKB(request.getParameter("shoukuanzhanghao"))){shoukuanzhanghao=request.getParameter("shoukuanzhanghao").trim();}else{jsonObject.put("msg", "shoukuanzhanghao 错误");jsonObject.put("error", "1");return jsonObject.toString();}
			if(UtsReg.getStringKB(request.getParameter("dakuanzhanghao"))){dakuanzhanghao=request.getParameter("dakuanzhanghao").trim();}else{jsonObject.put("msg", "名称 错误");jsonObject.put("error", "1");return jsonObject.toString();}
			if(UtsReg.getDouble(request.getParameter("price"))){price=request.getParameter("price").trim();}else{jsonObject.put("msg", "应结款 错误");jsonObject.put("error", "1");return jsonObject.toString();}
			if(UtsReg.getDouble(request.getParameter("usersid"))){usersid=request.getParameter("usersid").trim();}
			if(UtsReg.getDouble(request.getParameter("chaozuoyuanid"))){chaozuoyuanid=request.getParameter("chaozuoyuanid").trim();}
			
			MoneyRecord moneyrecord=new MoneyRecord();
			moneyrecord.setUsersid(usersid);
			moneyrecord.setDakuanzhanghao(dakuanzhanghao);
			moneyrecord.setShoukuanzhanghao(shoukuanzhanghao);
			moneyrecord.setPrice(price);
			moneyrecord.setChaozuoyuanid(chaozuoyuanid);
			
			if(!moneyrecordService.insert(moneyrecord)){
				jsonObject.put("msg", "失败");jsonObject.put("error", "1");return jsonObject.toString();
			}
			jsonObject.put("msg", "成功");
			jsonObject.put("error", "0");
			return jsonObject.toString();


		}
		
		//修改
		@RequestMapping("/sys/moneyrecord/up")
		@ResponseBody
		public String sys_moneyrecord_up(HttpServletRequest request) {
			JSONObject jsonObject = new JSONObject();
			String id="";
			String price="";
			String usersid="";
			String chaozuoyuanid="";
			
			if(UtsReg.getStringKB(request.getParameter("id"))){id=request.getParameter("id").trim();}else{jsonObject.put("msg", "id 错误");jsonObject.put("error", "1");return jsonObject.toString();}
			if(UtsReg.getStringKB(request.getParameter("price"))){price=request.getParameter("price").trim();}
			if(UtsReg.getStringKB(request.getParameter("usersid"))){usersid=request.getParameter("usersid").trim();}
			if(UtsReg.getStringKB(request.getParameter("chaozuoyuanid"))){chaozuoyuanid=request.getParameter("chaozuoyuanid").trim();}
			
			MoneyRecord moneyrecord=new MoneyRecord();
			moneyrecord.setId(Integer.valueOf(id));
			moneyrecord.setUsersid(usersid);
			moneyrecord.setPrice(price);
			moneyrecord.setChaozuoyuanid(chaozuoyuanid);
			
			if(!moneyrecordService.updateById(moneyrecord)){
				jsonObject.put("msg", "失败");jsonObject.put("error", "1");return jsonObject.toString();
			}
			jsonObject.put("msg", "成功");
			jsonObject.put("error", "0");
			return jsonObject.toString();


		}
		//删除
			@RequestMapping("/sys/moneyrecord/delbyid")
			@ResponseBody
			public String sys_moneyrecord_delbyid(HttpServletRequest request) {
				JSONObject jsonObject = new JSONObject();
		        String id="";
				
				if(UtsReg.getStringKB(request.getParameter("id"))){id=request.getParameter("id").trim();}else{jsonObject.put("msg", "来个id");jsonObject.put("error", "1");return jsonObject.toString();}
				
				
				
				if(moneyrecordService.selectCount(new EntityWrapper<MoneyRecord>().eq("id", id))<1){
					jsonObject.put("msg", "数据不存在");jsonObject.put("error", "1");return jsonObject.toString();
				}
				
				boolean ei=moneyrecordService.deleteById(Integer.valueOf(id));
				if(!ei){
					jsonObject.put("error", "1");jsonObject.put("msg", "失败");return jsonObject.toString();
				}
				
				
				jsonObject.put("msg", "成功");
				jsonObject.put("error", "0");
				return jsonObject.toString();


			}
	

			//管理员确认打款
			@RequestMapping("/app/MoneyRecord/add")
			@ResponseBody
			public String app_MoneyRecord_add(HttpServletRequest request) throws MalformedURLException, IOException {
				JSONObject jsonObject = new JSONObject();

				String token="";
				String id="";
				String dakuanrenid="";
				String dakuanrenname="";
				String dakuanrenimg="";
				String dakuanzhanghao="";
				String dakuanzhanghaohuzhu="";
				String dakuanyinhangname="";
				String dakuanyinhangaddress="";
			
				
				
				if(UtsReg.getStringKB(request.getParameter("token"))){token=request.getParameter("token").trim();}else{jsonObject.put("msg", "token 错误");jsonObject.put("error", "1");return jsonObject.toString();}
				if(UtsReg.getIntXl(request.getParameter("id"))){id=request.getParameter("id").trim();}else{jsonObject.put("msg", "id 错误");jsonObject.put("error", "1");return jsonObject.toString();}
				if(UtsReg.getStringKB(request.getParameter("dakuanrenid"))){dakuanrenid=request.getParameter("dakuanrenid").trim();}else{jsonObject.put("msg", "打款人id 错误");jsonObject.put("error", "1");return jsonObject.toString();}
				if(UtsReg.getStringKB(request.getParameter("dakuanrenname"))){dakuanrenname=request.getParameter("dakuanrenname").trim();}else{jsonObject.put("msg", "打款人名称 错误");jsonObject.put("error", "1");return jsonObject.toString();}
				
				if(UtsReg.getStringKB(request.getParameter("dakuanrenimg"))){dakuanrenimg=request.getParameter("dakuanrenimg").trim();}else{jsonObject.put("msg", "打款人头像  错误");jsonObject.put("error", "1");return jsonObject.toString();}
				if(UtsReg.getStringKB(request.getParameter("dakuanzhanghao"))){dakuanzhanghao=request.getParameter("dakuanzhanghao").trim();}else{jsonObject.put("msg", "打款账号 错误");jsonObject.put("error", "1");return jsonObject.toString();}
				if(UtsReg.getStringKB(request.getParameter("dakuanzhanghaohuzhu"))){dakuanzhanghaohuzhu=request.getParameter("dakuanzhanghaohuzhu").trim();}else{jsonObject.put("msg", "打款账号户主 错误");jsonObject.put("error", "1");return jsonObject.toString();}
				if(UtsReg.getStringKB(request.getParameter("dakuanyinhangname"))){dakuanyinhangname=request.getParameter("dakuanyinhangname").trim();}else{jsonObject.put("msg", "打款银行名称 错误");jsonObject.put("error", "1");return jsonObject.toString();}
				if(UtsReg.getStringKB(request.getParameter("dakuanyinhangaddress"))){dakuanyinhangaddress=request.getParameter("dakuanyinhangaddress").trim();}else{jsonObject.put("msg", "打款银行卡地址 错误");jsonObject.put("error", "1");return jsonObject.toString();}
				
				//验证token
				JSONObject yd_users = YD_Users.getUsersByToken(token);
				if (yd_users.getString("error").equals("1")) {jsonObject.put("msg", yd_users.getString("msg"));jsonObject.put("error", "1");return jsonObject.toString();}
				JSONObject users_token = JSONObject.parseObject(yd_users.getString("data"));
				
				
				EntityWrapper<MoneyRecord> ew=new EntityWrapper<MoneyRecord>();
				MoneyRecord MoneyRecord=moneyrecordService.selectOne(ew.eq("id",id));
				
				MoneyRecord.setChaozuoyuanid(users_token.getString("id"));
				MoneyRecord.setChaozuoyuanname(users_token.getString("chaozuoyuanname"));
				MoneyRecord.setChaozuoyuanimg(users_token.getString("chaozuoyuanimg"));
				MoneyRecord.setDakuanrenid(dakuanrenid);
				MoneyRecord.setDakuanrenname(dakuanrenname);
				MoneyRecord.setDakuanrenimg(dakuanrenimg);
				MoneyRecord.setDakuanzhanghao(dakuanzhanghao);
				MoneyRecord.setDakuanzhanghaohuzhu(dakuanzhanghaohuzhu);
				MoneyRecord.setDakuanyinhangname(dakuanyinhangname);
				MoneyRecord.setDakuanyinhangaddress(dakuanyinhangaddress);
				
				if(!moneyrecordService.insert(MoneyRecord)){
					jsonObject.put("msg", "失败");jsonObject.put("error", "1");return jsonObject.toString();
				}
				MoneyRecord.setState("1");
				if(!moneyrecordService.updateById(MoneyRecord)){
					jsonObject.put("msg", "失败");jsonObject.put("error", "1");return jsonObject.toString();
				}
				
				jsonObject.put("msg", "成功");
				jsonObject.put("error", "0");
				return jsonObject.toString();
			}

	
	
	
}
